#! /usr/bin/env python
# -*- coding: utf-8 -*-
from redis import StrictRedis
from utils.auth_check import decryption
from conf import setting

api_key_record = {}


def get_salt_pwd():
    salt_pwd = 'abc'
    return salt_pwd


class RedisServer(object):

    def _redis_connect(self):
        conn = StrictRedis(host=setting.REDIS_HOST,
                           port=setting.REDIS_PORT,
                           db=setting.REDIS_DB)

        return conn

    def check_auth(self, salt_pwd):
        return decryption(salt_pwd,api_key_record)

    def get(self, salt_pwd, name):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            str_ = conn.get(name)
            str_ = str(str_) if str_ else ""
            return str_
        return ""

    def set_(self, salt_pwd, name, value):

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.set(name, value)
            return bool_
        return False

    def getset(self, salt_pwd, name, value):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            str_ = conn.getset(name, value)
            str_ = str(str_) if str_ else ""
            return str_
        return ""

    def mget(self, salt_pwd, list_keys):
        def change(item):
            return item.decode('utf-8')

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.mget(list_keys)
            str_ = map(change, list_) if list_ else []
            return str_
        return []

    def setnx(self, salt_pwd, name, value):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.setnx(name, value)
            return bool_
        return False

    def setex(self, salt_pwd, name, time, value):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.setex(name, time, value)
            return bool_
        return False

    def keys(self, salt_pwd, pattern):
        def change(item):
            return str(item.decode('utf-8'))

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.keys(pattern)
            resp = list(map(change, list_)) if list_ else []
            return resp
        return []

    def type(self, salt_pwd, name):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            type_str = conn.type(name)
            return str(type_str.decode('utf-8'))
        return ""

    def delete_(self, salt_pwd, name):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            num_int = conn.delete(name)
            return num_int
        return 0

    def exists(self, salt_pwd, name):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.exists(name)
            return bool_
        return False

    def flushdb(self, salt_pwd):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.flushdb()
            return bool_
        return False

    def flushall(self, salt_pwd):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            bool_ = conn.flushall()
            return bool_
        return False

    def llen(self, salt_pwd, name):
        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            num_int = conn.llen(name)
            return num_int
        return 0

    def lrange(self, salt_pwd, name, start, len):
        def change(item):
            return str(item.decode('utf-8'))

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.lrange(name, start, len)
            resp = list(map(change, list_)) if list_ else []
            return resp
        return []

    def smembers(self, salt_pwd, name):
        def change(item):
            return str(item.decode('utf-8'))

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.smembers(name)
            resp = set(map(change, list_)) if list_ else ()
            return resp
        return ()

    def hkeys(self, salt_pwd, name):
        def change(item):
            return str(item.decode('utf-8'))

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.hkeys(name)
            resp = list(map(change, list_)) if list_ else []
            return resp
        return []

    def hvals(self, salt_pwd, name):
        def change(item):
            return str(item.decode('utf-8'))

        flag = self.check_auth(salt_pwd)
        if flag:
            conn = self._redis_connect()
            list_ = conn.hvals(name)
            resp = list(map(change, list_)) if list_ else []
            return resp
        return []
